/*
 * @lc app=leetcode.cn id=307 lang=cpp
 *
 * [307] 区域和检索 - 数组可修改
 */
#include "include.h"
// @lc code=start
class NumArray {
public:
    NumArray(vector<int>& nums) {
        numsPtr_ = &nums;
        length_ = nums.size();
        prefixSum_.resize(length_, 0);

        int sum = 0;
        for (int i=0;i<length_;++i){
            sum += nums[i];
            prefixSum_[i] = sum;
        }
    }
    
    void update(int index, int val) {
        int delta = val - (*numsPtr_)[index];
        for (int i=index;i<length_;++i){
            prefixSum_[i] += delta;
        }
        (*numsPtr_)[index] = val;
// for (int num : (*numsPtr_)){
//     printf("%d ", num);
// }
// printf("\n");
    }
    
    int sumRange(int left, int right) {
        if (left == 0){return prefixSum_[right];}
        return prefixSum_[right] - prefixSum_[left-1];
    }
private:
    std::vector<int>* numsPtr_;
    std::vector<int> prefixSum_;
    int length_;
};

/**
 * Your NumArray object will be instantiated and called as such:
 * NumArray* obj = new NumArray(nums);
 * obj->update(index,val);
 * int param_2 = obj->sumRange(left,right);
 */
// @lc code=end

